<?php
class LuminousGrammarBNF extends LuminousGrammar
{
  public function __construct()
  {
    $this->SetInfoAuthor( 
    array('name'=>'Mark Watkinson', 'email'=>'markwatkinson@gmail.com',
                         'website'=>'http://www.asgaard.co.uk'));
    $this->SetInfoLanguage('bnf');
    $this->SetInfoVersion('0.4.0');
    $this->operators = array();
  }
  
  private function SetCommonRules()
  {
    $this->delimited_types = 
      array_merge($this->delimited_types,
        array( luminous_generic_string("'", "'"),
               luminous_generic_string("\"", "\"")
        )
      );
      
  }
  
  private function SetStrictRuleset()
  {
    $this->delimited_types = array_merge($this->delimited_types,
    array(
      new LuminousDelimiterRule(0, 'COMMENT', 0, '&lt;!', '&gt;'),
      new LuminousDelimiterRule(0, 'KEYWORD', 0, '&lt;', '&gt;')
      )
    );
    $this->operators = array('::=', '\|');
  }
  
  private function SetExtendedRuleset()
  {
    /** TODO When luminous supports proper state transitions, put the other
     * markers in here. Putting them in now will not allow nesting.
     */ 
    $this->delimited_types = array_merge($this->delimited_types,
      array(
        new LuminousDelimiterRule(0, 'COMMENT', 0, '(*', '*)')
        
        
      )
    );
    
    $this->operators = array(',', ';', '\|', '=', '\*', '(?<!\s)\?');
    $this->simple_types[] = new LuminousSimpleRule(0, 'KEYWORD', 
      LUMINOUS_REGEX, '/(^[\s]*)(.*?)([\s]*=)/m', null, 2);
    
  }
  
  
  public function SetRuleset(&$str)
  {
    $this->SetCommonRules();
    
    $s = explode("\n", $str);
    $set = false;
    foreach($s as $line)
    {
      if (preg_match("/^[\s]*&lt;[a-z0-9\-_]+&gt;[\s]*\:\:\=/i", $line))
      {
        $this->SetStrictRuleset();
        $set = true;
      }
      elseif(preg_match("/^[\s]*[a-z_\-0-9 \t]+=/i", $line))
      {
        $this->SetExtendedRuleset();
        $set = true;
      }
      
      if ($set)
        break;
    }
    
    if ($set)
      $this->SetSimpleTypeRules();
    
  }
}